package cn.schoolwow.workflow.module.instance.action.service;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.database.dql.condition.Condition;
import cn.schoolwow.quickdao.domain.transaction.Transaction;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.dto.FlowContextDataRange;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.entity.WorkFlowInstanceHistory;
import cn.schoolwow.workflow.entity.WorkFlowTask;
import cn.schoolwow.workflow.module.instance.query.flow.GetWorkFlowInstanceConditionFlow;

import java.util.List;

public class DeleteWorkFlowInstanceServiceFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setInstanceIdList(flowContext);

        if(flowContext.containInstanceKey(Transaction.class)){
            deleteInstanceHistory(flowContext);
            deleteInstanceTask(flowContext);
            deleteInstance(flowContext);
        }else{
            DAO dao = flowContext.checkInstanceData(DAO.class);

            dao.startTransaction(transaction -> {
                flowContext.putInstanceData(transaction, Transaction.class, FlowContextDataRange.CurrentFlow);
                deleteInstanceHistory(flowContext);
                deleteInstanceTask(flowContext);
                deleteInstance(flowContext);
            });
        }
    }

    @Override
    public String name() {
        return "删除工作流实例服务流程";
    }

    private void setInstanceIdList(FlowContext flowContext){
        flowContext.executeFlowList(new GetWorkFlowInstanceConditionFlow());
        Condition condition = flowContext.checkInstanceData(Condition.class);

        List<Long> instanceIdList = condition.execute().getSingleColumnList(Long.class);
        if(null==instanceIdList||instanceIdList.isEmpty()){
            flowContext.brokenCurrentFlow("实例id查询为空");
        }
        flowContext.putCurrentFlowData("instanceIdList", instanceIdList);
    }

    private void deleteInstanceHistory(FlowContext flowContext){
        List<Long> instanceIdList = flowContext.checkData("instanceIdList", List.class);
        Transaction transaction = flowContext.checkInstanceData(Transaction.class);

        transaction.addRecordString("根据实例id列表删除工作流实例历史操作记录");
        transaction.query(WorkFlowInstanceHistory.class)
                .addInQuery("workFlowInstanceId", instanceIdList)
                .execute()
                .delete();
    }

    private void deleteInstanceTask(FlowContext flowContext){
        List<Long> instanceIdList = flowContext.checkData("instanceIdList", List.class);
        Transaction transaction = flowContext.checkInstanceData(Transaction.class);

        transaction.addRecordString("根据实例id列表删除工作流任务");
        transaction.query(WorkFlowTask.class)
                .addInQuery("instanceId", instanceIdList)
                .execute()
                .delete();
    }

    private void deleteInstance(FlowContext flowContext){
        List<Long> instanceIdList = flowContext.checkData("instanceIdList", List.class);
        Transaction transaction = flowContext.checkInstanceData(Transaction.class);

        transaction.addRecordString("根据实例id列表删除工作流实例");
        transaction.query(WorkFlowInstance.class)
                .addInQuery("id", instanceIdList)
                .execute()
                .delete();
    }
}
